package com.dy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/4/9 10:12
 * @Version 1.0 (版本)
 */
@RestController
public class DiscoveryController {
    @Autowired
    private DiscoveryClient client;
    @RequestMapping("/Discovery")
    //让注册进来的微服务，获取一些信息
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:"+services);

        //查询一个具体的微服务信息，通过具体的微服务id，applicaioninName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-8081");

        for (ServiceInstance instance:
                instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                            instance.getPort()+"\t"+
                            instance.getUri()+"\t"+
                            instance.getServiceId()
            );
        }
        return this.client;
    }
}
